home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1460.dms / var1460.adf / VSprites / Example2.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  11KB  |  356 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: VSprites                    Tulevagen 22       */
  8. /* File:    Example2.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to use several VSprites */
  21. /* each with its own colour table.                       */
  22.  
  23.  
  24.  
  25. /* Since we use Intuition, include this file: */
  26. #include <intuition/intuition.h>
  27.  
  28. /* Include this file since you are using sprites: */
  29. #include <graphics/gels.h>
  30.  
  31.  
  32.  
  33. /* We will use 15 VSprites: */
  34. #define MAXVSPRITES 15
  35.  
  36. /* They will move two pixels each time: */
  37. #define SPEED 2
  38.  
  39.  
  40.  
  41. /* Declare the functions we are going to use: */
  42. void main();
  43. void clean_up();
  44.  
  45.  
  46.  
  47. struct IntuitionBase *IntuitionBase = NULL;
  48. /* We need to open the Graphics library since we are using sprites: */
  49. struct GfxBase *GfxBase = NULL;
  50.  
  51.  
  52. /* Declare a pointer to a Screen structure: */ 
  53. struct Screen *my_screen;
  54.  
  55. /* Declare and initialize your NewScreen structure: */
  56. struct NewScreen my_new_screen=
  57. {
  58.   0,            /* LeftEdge  Should always be 0. */
  59.   0,            /* TopEdge   Top of the display.*/
  60.   320,          /* Width     We are using a low-resolution screen. */
  61.   200,          /* Height    Non-Interlaced NTSC (American) display. */
  62.   2,            /* Depth     4 colours. */
  63.   0,            /* DetailPen Text should be drawn with colour reg. 0 */
  64.   1,            /* BlockPen  Blocks should be drawn with colour reg. 1 */
  65.   SPRITES,      /* ViewModes No special modes. (Low-res, Non-Interlaced) */
  66.   CUSTOMSCREEN, /* Type      Your own customized screen. */
  67.   NULL,         /* Font      Default font. */
  68.   "MY SCREEN",  /* Title     The screen' title. */
  69.   NULL,         /* Gadget    Must for the moment be NULL. */
  70.   NULL          /* BitMap    No special CustomBitMap. */
  71. };
  72.  
  73.  
  74.  
  75. /* Declare a pointer to a Window structure: */ 
  76. struct Window *my_window = NULL;
  77.  
  78. /* Declare and initialize your NewWindow structure: */
  79. struct NewWindow my_new_window=
  80. {
  81.   0,             /* LeftEdge    x position of the window. */
  82.   0,             /* TopEdge     y positio of the window. */
  83.   320,           /* Width       320 pixels wide. */
  84.   200,           /* Height      200 lines high. */
  85.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  86.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  87.   CLOSEWINDOW,   /* IDCMPFlags  The window will give us a message if the */
  88.                  /*             user has selected the Close window gad. */
  89.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  90.   WINDOWCLOSE|   /*             Close Gadget. */
  91.   WINDOWDRAG|    /*             Drag gadget. */
  92.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  93.   WINDOWSIZING|  /*             Sizing Gadget. */
  94.   ACTIVATE,      /*             The window should be Active when opened. */
  95.   NULL,          /* FirstGadget No Custom gadgets. */
  96.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  97.   "VSprites are great!",        /* Title */
  98.   NULL,          /* Screen      Will later be connected to a custom scr. */
  99.   NULL,          /* BitMap      No Custom BitMap. */
  100.   80,            /* MinWidth    We will not allow the window to become */
  101.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  102.   320,           /* MaxWidth    than 320 x 200. */
  103.   200,           /* MaxHeight   */
  104.   CUSTOMSCREEN   /* Type        Connected to the Workbench Screen. */
  105. };
  106.  
  107.  
  108.  
  109. /* 1. Declare and initialize some sprite data: */
  110. UWORD chip vsprite_data[]=
  111. {
  112.   0x0180, 0x0000,
  113.   0x03C0, 0x0000,
  114.   0x07E0, 0x0000,
  115.   0x0FF0, 0x0000,
  116.   0x1FF8, 0x0000,
  117.   0x3FFC, 0x0000,
  118.   0x7FFE, 0x0000,
  119.   0x0000, 0xFFFF,
  120.   0x0000, 0xFFFF,
  121.   0x7FFE, 0x7FFE,
  122.   0x3FFC, 0x3FFC,
  123.   0x1FF8, 0x1FF8,
  124.   0x0FF0, 0x0FF0,
  125.   0x07E0, 0x07E0,
  126.   0x03C0, 0x03C0,
  127.   0x0180, 0x0180,
  128. };
  129.  
  130.  
  131.  
  132. /* 2. Declare three VSprite structures. One will be used, */
  133. /*    the other two are "dummies":                        */
  134. struct VSprite head, tail, vsprite[ MAXVSPRITES ];
  135.  
  136.  
  137. /* 3. Declare the VSprites' colour tables:     */
  138. WORD colour_table[ MAXVSPRITES ][ 3 ];
  139.  
  140.  
  141. /* 4. Declare a GelsInfo structure: */
  142. struct GelsInfo ginfo;
  143.  
  144.  
  145. /* This boolean variable will tell us if the VSprites are */
  146. /* in the list or not:                                    */
  147. BOOL vsprite_on = FALSE;
  148.  
  149.  
  150. /* This program will not open any console window if run from */
  151. /* Workbench, but we must therefore not print anything.      */
  152. /* Functions like printf() must therefore not be used.       */
  153. void _main()
  154. {
  155.   /* The GelsInfo structure needs the following arrays: */
  156.   WORD nextline[ 8 ];
  157.   WORD *lastcolor[ 8 ];
  158.  
  159.   /* Direction of the sprite: */
  160.   WORD x_direction[ MAXVSPRITES ];
  161.   WORD y_direction[ MAXVSPRITES ];
  162.  
  163.   /* Boolean variable used for the while loop: */
  164.   BOOL close_me = FALSE;
  165.  
  166.   /* Declare a pointer to an IntuiMessage structure: */
  167.   struct IntuiMessage *my_message;
  168.  
  169.   /* Used as counter in the for loop: */
  170.   UBYTE loop;
  171.  
  172.  
  173.  
  174.   /* Open the Intuition Library: */
  175.   IntuitionBase = (struct IntuitionBase *)
  176.     OpenLibrary( "intuition.library", 0 );
  177.   
  178.   if( IntuitionBase == NULL )
  179.     clean_up(); /* Could NOT open the Intuition Library! */
  180.  
  181.  
  182.  
  183.   /* 5. Open the Graphics Library:                                    */
  184.   /* Since we are using sprites we need to open the Graphics Library: */
  185.   /* Open the Graphics Library: */
  186.   GfxBase = (struct GfxBase *)
  187.     OpenLibrary( "graphics.library", 0);
  188.  
  189.   if( GfxBase == NULL )
  190.     clean_up(); /* Could NOT open the Graphics Library! */
  191.  
  192.  
  193.  
  194.   /* We will now try to open the screen: */
  195.   my_screen = (struct Screen *) OpenScreen( &my_new_screen );
  196.  
  197.   /* Have we opened the screen succesfully? */
  198.   if(my_screen == NULL)
  199.     clean_up();
  200.  
  201.  
  202.   my_new_window.Screen = my_screen;
  203.  
  204.  
  205.   /* We will now try to open the window: */
  206.   my_window = (struct Window *) OpenWindow( &my_new_window );
  207.   
  208.   /* Have we opened the window succesfully? */
  209.   if(my_window == NULL)
  210.     clean_up(); /* Could NOT open the Window! */
  211.  
  212.  
  213.  
  214.   /* 6. Initialize the GelsInfo structure: */
  215.  
  216.   /* All sprites except the first two may be used to draw */
  217.   /* the VSprites: ( 11111100 = 0xFC )                    */
  218.   ginfo.sprRsrvd = 0xFC;
  219.   /* If we do not exclude the first two sprites, the mouse */
  220.   /* pointer's colours may be affected.                    */
  221.  
  222.  
  223.   /* Give the GelsInfo structure some memory: */
  224.   ginfo.nextLine = nextline;
  225.   ginfo.lastColor = lastcolor;
  226.  
  227.  
  228.   /* Give the Rastport a pointer to the GelsInfo structure: */
  229.   my_window->RPort->GelsInfo = &ginfo;
  230.  
  231.   
  232.   /* Give the GelsInfo structure to the system: */
  233.   InitGels( &head, &tail, &ginfo );
  234.  
  235.  
  236.  
  237.  
  238.   /* 7. Initialize the VSprite structures: */
  239.  
  240.   /* Set a random seed: */
  241.   srand( 64 );
  242.  
  243.   for( loop = 0; loop < MAXVSPRITES; loop++ )
  244.   {
  245.     /* Set the VSprite's colours: */
  246.     colour_table[ loop ][ 0 ] = loop;      /* Blue  */
  247.     colour_table[ loop ][ 1 ] = loop << 4; /* Green */
  248.     colour_table[ loop ][ 2 ] = loop << 8; /* Red   */
  249.     
  250.     /* Set the speed and direction of the VSprite: */
  251.     x_direction[ loop ] = SPEED;
  252.     y_direction[ loop ] = -SPEED;
  253.  
  254.     vsprite[ loop ].Flags = VSPRITE;    /* It is a VSprite.    */
  255.     vsprite[ loop ].X = 10 + 20 * loop; /* X position.         */
  256.     vsprite[ loop ].Y = 10 + 20 * loop; /* Y position.         */
  257.     vsprite[ loop ].Height = 16;        /* 16 lines tall.      */
  258.     vsprite[ loop ].Width = 2;          /* 2 words wide.       */
  259.     vsprite[ loop ].Depth = 2;          /* 2 bitpl, 4 colours. */
  260.  
  261.     /* Pointer to the sprite data: */
  262.     vsprite[ loop ].ImageData = vsprite_data;
  263.  
  264.     /* Pointer to the colour table: */
  265.     vsprite[ loop ].SprColors = colour_table[ loop ];
  266.  
  267.  
  268.     /* 8. Add the VSprites to the VSprite list: */
  269.     AddVSprite( &vsprite[ loop ], my_window->RPort );
  270.   }
  271.   
  272.   
  273.   /* The VSprites are in the list. */
  274.   vsprite_on = TRUE;
  275.  
  276.  
  277.   /* Stay in the while loop until the user has */
  278.   /* selected the Close window gadget:         */
  279.   while( close_me == FALSE )
  280.   {
  281.     /* Stay in the while loop as long as we can collect messages: */
  282.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  283.     {
  284.       if( my_message->Class == CLOSEWINDOW)
  285.         close_me=TRUE;
  286.  
  287.       ReplyMsg( my_message );
  288.     }
  289.  
  290.     
  291.     /* Affect all VSprites: */
  292.     for( loop = 0; loop < MAXVSPRITES; loop++ )
  293.     {
  294.       /* Change the position of the VSprite: */
  295.       vsprite[ loop ].X += x_direction[ loop ];
  296.       vsprite[ loop ].Y += y_direction[ loop ];
  297.  
  298.  
  299.       /* Check that the sprite does not move outside the screen: */
  300.       if(vsprite[ loop ].X > 300)
  301.         x_direction[ loop ] = -SPEED;
  302.  
  303.       if(vsprite[ loop ].X < 0)
  304.         x_direction[ loop ] = SPEED;
  305.   
  306.       if(vsprite[ loop ].Y > 180)
  307.         y_direction[ loop ] = -SPEED;
  308.   
  309.       if(vsprite[ loop ].Y < 4)
  310.         y_direction[ loop ] = SPEED;
  311.     }
  312.   
  313.     /* 9. Sort the Gels list: */
  314.     SortGList( my_window->RPort );
  315.  
  316.     /* 10. Draw the Gels list: */
  317.     DrawGList( my_window->RPort, &(my_screen->ViewPort) );
  318.  
  319.     /* 11. Set the Copper and redraw the display: */
  320.     MakeScreen( my_screen );
  321.     RethinkDisplay();    
  322.   }
  323.  
  324.  
  325.  
  326.   /* Free all allocated memory: (Close the window, libraries etc) */
  327.   clean_up();
  328.  
  329.   /* THE END */
  330. }
  331.  
  332.  
  333.  
  334. /* This function frees all allocated memory. */
  335. void clean_up()
  336. {
  337.   UBYTE loop;
  338.   
  339.   if( vsprite_on )
  340.     for( loop = 0; loop < MAXVSPRITES; loop++ )
  341.       RemVSprite( &vsprite[ loop ] );
  342.  
  343.   if( my_window )
  344.     CloseWindow( my_window );
  345.   
  346.   if(my_screen )
  347.     CloseScreen( my_screen );
  348.  
  349.   if( GfxBase )
  350.     CloseLibrary( GfxBase );
  351.  
  352.   if( IntuitionBase )
  353.     CloseLibrary( IntuitionBase );
  354.  
  355.   exit();
  356. }